<?php
require_once('TVB/pms/pdes/action/InfoGetAction.class.php');

class InfoAddressUpdateAction extends BaseAction 
{
	/**
	 * @isnull false
	 */
	public $id;
	public $episode_id;

	/**
	 * @maxLength 255
	 * @label Venue Name (Chinese)
	 */
	public $venue_name_chinese;

	/**
	 * @maxLength 255
	 * @label Venue Name (English)
	 */
	public $venue_name_english;

	/**
	 * @filter number
	 * @maxLength 11
	 * @label Venue Category 
	 */
	public $venue_category;

	/**
	 * @maxLength 255
	 * @label Street Line 1
	 */
	public $street_line_1;

	/**
	 * @maxLength 255
	 * @label Street Line 2
	 */
	public $street_line_2;

	/**
	 * @maxLength 255
	 * @label City
	 */
	public $city;
	
	/**
	 * @maxLength 255
	 * @label Phone Number
	 */
	public $phone_number;

	/**
	 * @maxLength 255
	 * @label Fax Number
	 */
	public $fax_number;

	/**
	 * @maxLength 255
	 * @label Url
	 */
	public $url;

	/**
	 * @maxLength 255
	 * @label Email
	 */
	public $email;

	/**
	 * @maxLength 255
	 * @label Contact Person(s)
	 */
	public $contact_person;

	public $description;

	public $province_state;
	public $country;
	public $useful_information;
	
	/**
	 * @filter datetime
	 * @label Publish Date/Time
	 */
    public $publish_datetime;
	
	/**
	 * @filter datetime
	 * @label Expire Date/Time
	 */
    public $expire_datetime;
	
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;

	public $programme_id;
	public $onair_episode_no;

	#public $role_id;
	public $cast_id; # modify by vanni,for bug 7397

	public $name_type;
	public $name_last;
	public $name_first;
	public $alphabet;

	public $ut;
	public $reset;
	public $from;
	public function execute(){

		if (!is_null($this->reset))
		{
			$this->setData('id', $this->id);
			$this->setData('type', 'address');
			$this->setData('act', 'edit');
			$this->setData('from', $this->from);
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if (CommonUtils::is_concurrent('info_address', $this->id, $this->modified_at))
			{
				$this->init_page_data();
				$this->addError('concurrent', array('0105', 'Concurrent update found!'));
				return 'logic-error';
			}

			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->venue_name_chinese) && is_null($this->venue_name_english))
			{
				$this->init_page_data();
				$this->addError('Address.Venue Name', array('0043', 'Venue Name'));
				return 'filter-error';
			}

            //check publish_datetime && expire_datetime
            if (empty($this->publish_datetime) 
                || empty($this->expire_datetime) 
                || $this->publish_datetime > $this->expire_datetime) {
                
                $this->init_page_data();
                $this->addError('Publish Date/Time && Expire Date/Time', array('0033', 'Publish Date/Time && Expire Date/Time')); 
                return 'filter-error';
            }

			$update_id = $this->update_info_address();
			if ($update_id >= 0 )
			{
				$this->setData('type', 'address');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return ($this->from == 'task') ? "task" : "succ";
		}
		catch(Exception $e)
		{
			error_log("Update Address Fails: {$this->id}" . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();
			return 'system-error';
		}
	}    

	/**
	 * add info_address information
	 */
	private function update_info_address()
	{
		
		$info_address = new info_address();
		$info_address = $info_address->get($this->id);
		Model::diff($info_address, $this);
		$info_address->modified_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_address->modified_by = CommonUtils::get_user();
		$info_address->contact_person = CommonUtils::json_info_data($this, 'contact_person');
        $info_address->province_state_prefecture = $this->province_state;
        $info_address->venue_name_chinese = trim($this->venue_name_chinese);
        $info_address->venue_name_english = trim($this->venue_name_english);
        $info_address->city = trim($this->city);
        $info_address->phone_number = trim($this->phone_number);
        $info_address->fax_number = trim($this->fax_number);
        $info_address->url = trim($this->url);
        $info_address->email = trim($this->email);
        $info_address->street_line_1 = trim($this->street_line_1);
        $info_address->street_line_2 = trim($this->street_line_2);
		$info_address->description = trim($this->description);
        $info_address->publish_datetime = $this->publish_datetime; 
        $info_address->expire_datetime = $this->expire_datetime; 

		$update_id = Model::$dbo['w']->save($info_address);
		if ($update_id > 0)
		{
            $primary_key = array(
                'id'    => $this->id
            );
            $fields = array(
                'venue_name_chinese',
                'venue_name_english',
                'street_line_1',
                'street_line_2',
                'city',
                'phone_number',
                'fax_number',
                'url',
                'email',
                'contact_person',
                'useful_information',
                'description'
            );
            CommonUtils::filterC2A0('info_address', $primary_key, $fields);

			$info_address->add_change_log();
			$this->addInfo("info_address:".AC::LOG_OP_UPDATE.":".$this->id, "update info_address success", json_encode($info_address));
			return $update_id;
		}
		else
		{
			$this->addError("info_address:".AC::LOG_OP_UPDATE.":".$this->id, "update info_address error", json_encode($info_address));
			return false;
		}

	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$action = new InfoGetAction();
		$action->id = $this->id;
		$action->act = 'edit';
		$action->type = 'address';
		$action->execute();

		$old_data = $action->getData();
		$this->modified_at = $old_data['address']->modified_at;
		$old_data['address'] = Model::diff($old_data['address'], $this);

		$old_data['address']->country_name  = country::getCountryName($this->country);
		$old_data['address']->province_name = province_state_prefecture::getProvinceName($this->province_state);
		$old_data['address']->province_state_prefecture = !empty($this->province_state) ? $this->province_state : '';
		foreach ($old_data as $object=>$data)
		{
			$this->setData($object, $data);
		}

		$json_fields = AC::$infoJson['info_address'];
		$json_data = array();
		foreach ($json_fields as $field)
		{
			$json_decode_data = json_decode(CommonUtils::json_info_data($this, $field), true);
			$json_data[$field] = isset($json_decode_data[$field]) ? $json_decode_data[$field] : array();
		}
		$this->setData("address_json_list", $json_data);

		//jump page
		$this->setData('id', $this->id);
		$this->setData('type', 'address');
		$this->setData('act', 'edit');
		$this->setData('from', $this->from);
	}
}
?>
